package com.jingxi.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jingxi.common.pojo.EasyUIDataGridResult;
import com.jingxi.common.pojo.JingXiResult;
import com.jingxi.model.TbContentCategory;
import com.jingxi.service.ContentCatService;

@Controller
@RequestMapping("/content/category")
public class ContentCatController {

	@Autowired
	private ContentCatService contentCatService;
	
	@SuppressWarnings({"rawtypes","unchecked"})
	@RequestMapping("/list")
	@ResponseBody
	public List categoryList(@RequestParam(value="id",defaultValue="0") Long parentId) throws Exception{
		List catList = new ArrayList();
		//查询数据库
		List<TbContentCategory> list = contentCatService.getContentCatList(parentId);
		for(TbContentCategory tbItemCat : list) {
			Map node = new HashMap<>();
			node.put("id", tbItemCat.getId());
			node.put("text",tbItemCat.getName());
			node.put("state",tbItemCat.getIsParent()?"closed":"open");
			catList.add(node);
		}
		return catList;
	}
	
	@RequestMapping("/create")
	@ResponseBody
	public JingXiResult createContentCat(long parentId,String name) {
		return contentCatService.createContentCat(parentId, name);
	}
	

	@RequestMapping("/update")
	@ResponseBody
	public JingXiResult updateContentCat(long id,String name) {
		return contentCatService.updateContentCat(id, name);
	}
	
	@RequestMapping("/delete/")
	@ResponseBody
	public JingXiResult deleteContentCat(long id) {
		return contentCatService.deleteContentCat(id);
	}
}
